package cn.ddiancan.schedule.base.controller;


import java.util.List;

import cn.ddiancan.schedule.base.domain.TaskDTO;
import cn.ddiancan.schedule.base.domain.TaskLogVO;
import cn.ddiancan.schedule.base.service.ITaskTimerLogService;
import cn.ddiancan.schedule.base.service.ITaskTimerService;
import cn.ddiancan.xddcloud.common.entity.CommonResult;
import cn.ddiancan.xddcloud.ddccore.request.PageRequest;
import cn.ddiancan.xddcloud.ddccore.response.PagedResultListInfo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * 定时任务记录信息
 *
 * @author Amos
 */
@RestController
@Tag(name = "TaskTimerController", description = "定时任务")
public class TaskTimerController {

    @Autowired
    private ITaskTimerService taskTimerService;

    @Autowired
    private ITaskTimerLogService taskTimerLogService;

    /**
     * 新增任务
     *
     * @param taskDTO 入参
     * @return ResultDTO 返回
     */
    @PostMapping("/add")
    @Operation(summary = "addTaskTimer", description = "新增任务")
    public CommonResult<Integer> addTaskTimer(@RequestBody TaskDTO taskDTO) {
        try {
            return taskTimerService.addTaskTimer(taskDTO);
        } catch (Exception e) {
            return CommonResult.failed(e.getMessage());
        }
    }

    /**
     * 任务删除
     *
     * @param recordId
     * @return
     */
    @DeleteMapping("/delete/{recordId}")
    @Operation(summary = "deleteTaskTimer", description = "任务删除")
    public CommonResult<Integer> deleteTaskTimer(@PathVariable("recordId") Long recordId) {
        CommonResult<Integer> resultDTO = new CommonResult();
        try {
            int data = taskTimerService.deleteTaskTimer(recordId);
            resultDTO.setCode(HttpStatus.OK.value());
            resultDTO.setMsg("success");
            resultDTO.setData(data);
        } catch (Exception e) {
            resultDTO.setCode(HttpStatus.INTERNAL_SERVER_ERROR.value());
            resultDTO.setMsg(e.getMessage());
        }
        return resultDTO;
    }

    /**
     * 更新任务
     *
     * @param taskDTO
     * @return
     */
    @PutMapping("/update")
    @Operation(summary = "updateTaskTimer", description = "更新任务")
    public CommonResult<Integer> updateTaskTimer(@RequestBody TaskDTO taskDTO) {
        try {
            return taskTimerService.updateTaskTimer(taskDTO);
        } catch (Exception e) {
            return CommonResult.failed(e.getMessage());
        }
    }

    /**
     * 分页获取任务清单
     *
     * @param pageRequest 入参
     * @return PageDataDTO
     */
    @PostMapping("/list/page")
    @Operation(summary = "searchTaskTimer", description = "分页获取任务清单")
    public PagedResultListInfo<TaskDTO> searchTaskTimer(@RequestBody PageRequest<TaskDTO> pageRequest) {
        return taskTimerService.getTaskTimer(pageRequest);
    }

    /**
     * 分页获取任务日志清单
     *
     * @param pageRequest 入参
     * @return PageDataDTO
     */
    @PostMapping("/list/log/page")
    @Operation(summary = "searchTaskTimerLog", description = "分页获取任务日志清单")
    public PagedResultListInfo<TaskLogVO> searchTaskTimerLog(@RequestBody PageRequest<TaskLogVO> pageRequest) {
        return taskTimerLogService.getTaskTimerLogList(pageRequest);
    }

    /**
     * 获取可用的任务列表
     *
     * @return ResultDTO
     */
    @GetMapping("/list/valid")
    @Operation(summary = "searchEnableTaskTimer", description = "获取可用的任务列表")
    public CommonResult<List<TaskDTO>> searchEnableTaskTimer() {
        List<TaskDTO> taskTimer = taskTimerService.getAllEnableTaskTimer();
        return CommonResult.success(taskTimer);
    }

    /**
     * 手动触发
     *
     * @param ids
     * @return
     */
    @PostMapping("/execute/manual")
    @Operation(summary = "executeTaskTimer", description = "手动触发")
    public CommonResult<List<Long>> executeTaskTimer(@RequestBody List<Long> ids) {
        taskTimerService.executeTaskTimer(ids);
        return CommonResult.success(ids);
    }

    /**
     * 启用
     *
     * @param ids
     * @return
     */
    @PostMapping("/execute/enable")
    @Operation(summary = "taskUP", description = "启用")
    public CommonResult<List<Long>> taskUp(@RequestBody List<Long> ids) {
        taskTimerService.taskUP(ids);
        return CommonResult.success(ids);
    }

    /**
     * 禁用
     *
     * @param ids
     * @return
     */
    @PostMapping("/execute/disable")
    @Operation(summary = "taskDown", description = "禁用/停用")
    public CommonResult<List<Long>> taskDown(@RequestBody List<Long> ids) {
        taskTimerService.taskDown(ids);
        return CommonResult.success(ids);
    }

    @PostMapping("/task/flush/{operationType}")
    @Operation(summary = "刷新任务", description = "刷新任务")
    public CommonResult<String> flushTaskTimer(@PathVariable("operationType") String operationType,
        @RequestBody TaskDTO taskDTO) {
        return taskTimerService.flushScheduleTask(taskDTO, operationType);
    }
}
